package com.ls.tree.threadedbinarytree;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        ThreadedBinaryTree tree = new ThreadedBinaryTree();
        HeroNode root = new HeroNode(1, "11");
        HeroNode node2 = new HeroNode(3, "33");
        HeroNode node3 = new HeroNode(6, "66");
        HeroNode node4 = new HeroNode(8, "88");
        HeroNode node5 = new HeroNode(10, "1010");
        HeroNode node6 = new HeroNode(14, "1414");

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        tree.setRoot(root);

        tree.threadedNodes();

        //测试10号
 /*       HeroNode left = node5.getLeft();
        HeroNode right = node5.getRight();
        System.out.println(left);
        System.out.println(right);*/

        tree.threadedList();
    }
}

//实现了线索化二叉树
class ThreadedBinaryTree {
    private HeroNode root;

    //为了实现线索化,需要创建要给指向当前节点的前驱节点的指针
    //在递归进行线索化时,pre总是保留前一个节点
    private HeroNode pre = null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void threadedNodes() {
        threadedNodes(root);
    }

    /**
     * 遍历线索化树
     */
    public void threadedList() {
        HeroNode node = root;
        while (node != null) {
            //循环找到lefttype == 1的节点,第一个找到的是8
            while (node.getLeftType() == 0) {
                node = node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType() == 1) {
                node = node.getRight();
                System.out.println(node);
            }
            node = node.getRight();
        }


    }

    /**
     * 中序线索化
     *
     * @param node 当前线索化的节点
     */
    public void threadedNodes(HeroNode node) {
        if (node == null) {
            return;
        }
        //线索化左子树
        threadedNodes(node.getLeft());
        //线索化当前节点

        //处理当前节点的前驱节点
        if (node.getLeft() == null) {
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);
            pre.setRightType(1);
        }
        //!!!每处理一个节点后,让当前节点是下一个节点的前驱节点
        pre = node;
        //线索化右子树
        threadedNodes(node.getRight());
    }


    public void delNode(int no) {
        if (root != null) {
            if (root.getNo() == no) {
                root = null;
            } else {
                root.delNode(no);
            }
        } else {
            System.out.println("空树");
        }
    }

    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return root.postOrderSearch(no);
        } else {
            return null;
        }

    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        }
    }

    //后
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        }
    }
}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //1如果leftType == 0表示指向左子树,如果 ==1表示指向前驱节点
    //2如果rightType == 0表示指向右子树,如果 ==1表示指向后继节点
    private int leftType;
    private int rightType;


    //递归删除节点
    //1.如果删除的节点是叶子节点,则删除该节点
    //2.如果删除的节点是不是叶子节点,则删除该子树
    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }


    public HeroNode preOrderSearch(int no) {
        System.out.println("---");
        //比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        //1判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
        //2如果左递归前序查找,找到节点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }

        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;

    }

    public HeroNode infixOrderSearch(int no) {
        System.out.println("---");
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            return this;
        }

        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    public HeroNode postOrderSearch(int no) {
        System.out.println("---");
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

    //前序遍历
    public void preOrder() {
        System.out.println(this);//先输出父节点
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder() {

        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);//先输出父节点
        if (this.right != null) {
            this.right.infixOrder();
        }
    }


    //后
    public void postOrder() {

        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.infixOrder();
        }
        if (this.right != null) {
            this.right.infixOrder();
        }
        System.out.println(this);//先输出父节点
    }


    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}